//
//  CustomAnnotationView.m
//  CustomAnnotationDemo
//
//  Created by songjian on 13-3-11.
//  Copyright (c) 2013年 songjian. All rights reserved.
//

#import "CustomAnnotationView.h"
#import "CustomCalloutView.h"

#define kWidth  150.f
#define kHeight 60.f

#define kHoriMargin 5.f
#define kVertMargin 5.f

#define kPortraitWidth  50.f
#define kPortraitHeight 50.f

#define kCalloutWidth   171.0
#define kCalloutHeight  123.0

@interface CustomAnnotationView ()

@property (nonatomic, strong) UILabel *nameLabel;

@end

@implementation CustomAnnotationView

@synthesize calloutView;
@synthesize portraitImageView   = _portraitImageView;
@synthesize nameLabel           = _nameLabel;

#pragma mark - Handle Action

- (void)btnGPSAction
{
    CLLocationCoordinate2D coorinate = [self.annotation coordinate];
    
    if (self.cusdelegate&&[self.cusdelegate respondsToSelector:@selector(clickGPSWithLat:WithLong: WithTag:)]) {
        [self.cusdelegate clickGPSWithLat:[NSString stringWithFormat:@"%f",coorinate.latitude] WithLong:[NSString stringWithFormat:@"%f",coorinate.longitude] WithTag:self.tag];
    }
}
- (void)btnTelAction
{
    if (self.cusdelegate&&[self.cusdelegate respondsToSelector:@selector(clickTelWithTag:)]) {
        [self.cusdelegate clickTelWithTag:self.tag];
    }
}
- (void)dotap
{
    if (self.cusdelegate&&[self.cusdelegate respondsToSelector:@selector(clickDetail:)]) {
        [self.cusdelegate clickDetail:self.tag];
    }
}
#pragma mark - Override

- (NSString *)name
{
    return self.nameLabel.text;
}

- (void)setName:(NSString *)name
{
    self.nameLabel.text = name;
}

- (UIImage *)portrait
{
    return self.portraitImageView.image;
}

- (void)setPortrait:(UIImage *)portrait
{
    self.portraitImageView.image = portrait;
}

- (void)setSelected:(BOOL)selected
{
    [self setSelected:selected animated:NO];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    if (self.selected == selected)
    {
        return;
    }
    
    if (selected)
    {
        if (self.calloutView == nil)
        {
            /* Construct custom callout. */
            self.calloutView = [[CustomCalloutView alloc] initWithFrame:CGRectMake(0, 0, kCalloutWidth, kCalloutHeight)];
            self.calloutView.center = CGPointMake(CGRectGetWidth(self.bounds) / 2.f + self.calloutOffset.x,
                                                  -CGRectGetHeight(self.calloutView.bounds) / 2.f + self.calloutOffset.y);
            
            self.gpsGo = [UIButton buttonWithType:UIButtonTypeCustom];
            self.gpsGo.frame = CGRectMake(6, 80, 77, 25);
            [self.gpsGo setTitle:@"导航过去" forState:UIControlStateNormal];
            [self.gpsGo setTitleColor:[UIColor colorWithRed:79.0/255 green:193.0/255 blue:233.0/255 alpha:1] forState:UIControlStateNormal];
            self.gpsGo.titleLabel.font=[UIFont systemFontOfSize:15];
            self.gpsGo.titleLabel.layer.cornerRadius=2;
            [self.gpsGo addTarget:self action:@selector(btnGPSAction) forControlEvents:UIControlEventTouchUpInside];
            [self.gpsGo setBackgroundImage:[UIImage imageNamed:@"nearbyDrug_markButton.png"] forState:UIControlStateNormal];
            [self.calloutView addSubview:self.gpsGo];
            
            
            self.telGo = [UIButton buttonWithType:UIButtonTypeCustom];
            self.telGo.frame = CGRectMake(88, 80, 77, 25);
            self.telGo.titleLabel.font=[UIFont systemFontOfSize:15];
            [self.telGo setTitle:@"电话过去" forState:UIControlStateNormal];
            [self.telGo setTitleColor:[UIColor colorWithRed:79.0/255 green:193.0/255 blue:233.0/255 alpha:1] forState:UIControlStateNormal];
            [self.telGo addTarget:self action:@selector(btnTelAction) forControlEvents:UIControlEventTouchUpInside];
            [self.telGo setBackgroundImage:[UIImage imageNamed:@"nearbyDrug_markButton.png"] forState:UIControlStateNormal];
            [self.calloutView addSubview:self.telGo];
            
            UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(20, 40, 131, 1)];
            label.backgroundColor=[UIColor whiteColor];
            label.alpha=0.5;
            [self.calloutView addSubview:label];
            
            self.tel=[[UILabel alloc]initWithFrame:CGRectMake(15, 50, 141, 18)];
            self.tel.textColor=[UIColor whiteColor];
            self.tel.backgroundColor=[UIColor clearColor];
            self.tel.font=[UIFont systemFontOfSize:15];
            self.tel.textAlignment=NSTextAlignmentCenter;
            self.tel.text=@"010-12345678";
            [self.calloutView addSubview:self.tel];
            
            
            self.itemName=[[UILabel alloc]initWithFrame:CGRectMake(15, 10, 141, 18)];
            self.itemName.textColor=[UIColor whiteColor];
            self.itemName.backgroundColor=[UIColor clearColor];
            self.itemName.font=[UIFont systemFontOfSize:15];
            self.itemName.textAlignment=NSTextAlignmentCenter;
            self.itemName.text=@"朝阳医院（将台路）";
            [self.calloutView addSubview:self.itemName];
            
            
            UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dotap)];
            [self.calloutView addGestureRecognizer:tap];
        }
        [self addSubview:self.calloutView];
    }
    else
    {
        [self.calloutView removeFromSuperview];
    }
    
    [super setSelected:selected animated:animated];
}

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    BOOL inside = [super pointInside:point withEvent:event];
    /* Points that lie outside the receiver’s bounds are never reported as hits,
     even if they actually lie within one of the receiver’s subviews.
     This can occur if the current view’s clipsToBounds property is set to NO and the affected subview extends beyond the view’s bounds.
     */
    if (!inside && self.selected)
    {
        inside = [self.calloutView pointInside:[self convertPoint:point toView:self.calloutView] withEvent:event];
    }
    
    return inside;
}

#pragma mark - Life Cycle

- (id)initWithAnnotation:(id<MAAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
    
    if (self)
    {
        self.bounds = CGRectMake(0.f, 0.f, 14, 19);
        self.backgroundColor = [UIColor clearColor];
        /* Create portrait image view and add to view hierarchy. */
        self.portraitImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 14, 19)];
        self.portraitImageView.image = [UIImage imageNamed:@"nearbyDrug_markUnselect.png"];
        [self addSubview:self.portraitImageView];
    }
    return self;
}

@end
